$(function () {
    var bigcustomer_datagrid = $('#bigcustomer_datagrid');
    var bigcustomer_dialog = $("#bigcustomer_dialog");
    var bigcustomer_form = $("#bigcustomer_form");
    var combobox_role = $("#combobox_role");
    var aduit_btn = $("#aduit_btn");
    var update_btn = $("#update_btn");
    var delete_btn = $("#delete_btn");
    var bigcustomer_search_dialog = $("#bigcustomer_search_dialog");
    var bigcustomer_search_form = $("#bigcustomer_search_form");
    $('#bigcustomer_datagrid').datagrid({
        url: '/bigcustomer/list',
        fit: true,
        pagination: true,
        rownumbers: true,
        border: false,
        singleSelect: true,
        fitColumns: true,
        toolbar: '#bigcustomer_datagrid_button',
        columns: [[
            {field: 'schoolName', title: '学校名', width: 100},
            {field: 'location', title: '所在地', width: 100},
            {field: 'importance', title: '重要度', width: 100,formatter: formatimportance},
            {field: 'intentionDegree', title: '意向度', width: 100,formatter: formatintention},
            {field: 'contact', title: '联系人', width: 100,formatter: formatcontact},
            {field: 'schoolTel', title: '电话', width: 100},
            {field: 'subject', title: '意向学科', width: 100,formatter: formatintentionSubject},
            {field: 'marketer', title: '营销人员', width: 100,formatter: formatmarketer},
            {field: 'tracer', title: '跟踪人员', width: 100,formatter: formattracer},
            {field: 'earlierTime', title: '上次跟踪时间', width: 100,formatter: formatDate},
            {field: 'laterTime', title: '下次跟踪时间', width: 100,formatter: formatDate},
            {field: 'traceState', title: '跟踪状态', width: 100,formatter:formatTraceState},
            {field: 'state', title: '是否签约', width: 100,formatter: formatState},
        ]],
        onCheck:function(index,row){
            if(row.state==1){
                //未签约
                aduit_btn.linkbutton({
                    text:'解约',
                    disabled:false
                });
                update_btn.linkbutton({
                    disabled:true
                });
                delete_btn.linkbutton({
                    disabled:true
                });

            }else if (row.state==0){
                //签约了
                aduit_btn.linkbutton({
                    text:'签约',
                    disabled:false
                });
                update_btn.linkbutton({
                    disabled:false
                });
                delete_btn.linkbutton({
                    disabled:false
                });
            }else{
                //失效了
                update_btn.linkbutton({
                    disabled:true
                });
                delete_btn.linkbutton({
                    disabled:true
                });
                aduit_btn.linkbutton({
                    disabled:true
                });
            }
        }
    });

    var cmdObj = {
        search: function () {
            var keyword = $("#keyword").val();
            bigcustomer_datagrid.datagrid("load", {keyword: keyword});
        },
        addBigCustomer: function () {
            bigcustomer_form.form("clear");
            bigcustomer_dialog.dialog("setTitle", "添加");
            bigcustomer_dialog.dialog("open");
        },
        updateBigCustomer: function () {
            var bigcustomer = bigcustomer_datagrid.datagrid("getSelected");
            console.log(bigcustomer);

            if (!bigcustomer) {
                $.messager.alert('温馨提示', '请选中一行！', 'info');
                return;
            }
            $("#bigcustomer_form input").prop("disabled", false);

            bigcustomer_form.form("clear");
            bigcustomer_dialog.dialog("setTitle", "大客户编辑");

            //回显学校联系人
            if (bigcustomer.contact) {
                bigcustomer["contact.id"] = bigcustomer.contact.id;
            }
            //回显重要性
            if (bigcustomer.importance) {
                bigcustomer["importance.id"] = bigcustomer.importance.id;
            }
            //回显意向程度
            if (bigcustomer.intentionDegree) {
                bigcustomer["intentionDegree.id"] = bigcustomer.intentionDegree.id;
            }
            //回显意向学科
            if (bigcustomer.subject) {
                bigcustomer["subject.id"] = bigcustomer.subject.id;
            }
            //回显意向校区
            if (bigcustomer.college) {
                bigcustomer["college.id"] = bigcustomer.college.id;
            }
            //回显营销人员
            if (bigcustomer.marketer) {
                bigcustomer["marketer.id"] = bigcustomer.marketer.id;
            }
            //回显追踪人员
            if (bigcustomer.tracer) {
                bigcustomer["tracer.id"] =  bigcustomer.tracer.id;
            }
            //回显学校类型
            if (bigcustomer.type) {
                bigcustomer["type.id"] = bigcustomer.type.id;
            }
            bigcustomer_form.form("load", bigcustomer);

            bigcustomer_dialog.dialog("open");
        },
        //查询
        checkBigCustomer:function(){
            var bigcustomer = bigcustomer_datagrid.datagrid("getSelected");
            console.log(bigcustomer);

            if (!bigcustomer) {
                $.messager.alert('温馨提示', '请选中一行！', 'info');
                return;
            }
            $("#bigcustomer_search_form input").prop("disabled", true);
            bigcustomer_form.form("clear");
            bigcustomer_search_dialog.dialog("setTitle", "大客户查看");

            //回显学校联系人
            if (bigcustomer.contact) {
                bigcustomer["contact.id"] = bigcustomer.contact.id;
            }
            //回显重要性
            if (bigcustomer.importance) {
                bigcustomer["importance.id"] = bigcustomer.importance.id;
            }
            //回显意向程度
            if (bigcustomer.intentionDegree) {
                bigcustomer["intentionDegree.id"] = bigcustomer.intentionDegree.id;
            }
            //回显意向学科
            if (bigcustomer.subject) {
                bigcustomer["subject.id"] = bigcustomer.subject.id;
            }
            //回显意向校区
            if (bigcustomer.college) {
                bigcustomer["college.id"] = bigcustomer.college.id;
            }
            //回显营销人员
            if (bigcustomer.marketer) {
                bigcustomer["marketer.id"] = bigcustomer.marketer.id;
            }
            //回显追踪人员
            if (bigcustomer.tracer) {
                bigcustomer["tracer.id"] =  bigcustomer.tracer.id;
            }
            //回显学校类型
            if (bigcustomer.type) {
                bigcustomer["type.id"] = bigcustomer.type.id;
            }
            bigcustomer_search_form.form("load", bigcustomer);
            bigcustomer_search_dialog.dialog("open");

        },
        //签约
        signBigCustomer:function(){
            var bigcustomer = bigcustomer_datagrid.datagrid("getSelected");
            if (!bigcustomer) {
                $.messager.alert("温馨提示", "请选中一条数据!", "info");
                return;
            }
            var future = bigcustomer.state ? "取消签约" : "签约";
            var flag = bigcustomer.state ? 0 : 1;
            $.messager.confirm("确认窗口", "确认要跟该客户"+ future +"么?", function (yes) {
                if (yes) {
                    var id = bigcustomer_datagrid.datagrid("getSelected").id;
                    $.post("/bigcustomer/sign", {id: id, flag: flag}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","操作成功", "info", function () {
                                bigcustomer_datagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert("温馨提示", data.message, "info");
                        }
                    })
                }
            })

        },
        //删除大客户(转变签约状态)
        fireBigCustomer: function () {
            var selectedRow = bigcustomer_datagrid.datagrid("getSelected");
            if (!selectedRow) {
                $.messager.alert('警告', '请选择你要删除的一行', 'warning');
                return;
            } else {
                $.messager.confirm('温馨提示', '你确定要删除这条数据吗？', function (yes) {
                    if (yes) {
                        $.post("/bigcustomer/delete", {id: selectedRow.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', "以后还有机会", function () {
                                 cmdObj.reloadBigCustomer()
                                })
                            } else {
                                $.messager.alert('提示', data.msg, 'error');
                            }
                        })
                    }
                });
            }
        },
        //刷新大客户
        reloadBigCustomer: function () {
            bigcustomer_datagrid.datagrid("reload");
        },
        //保存
        save: function () {
            var url = "/bigcustomer/save";
            var urlId = $("#bigcustomer_id").val();
            if (urlId) {
                url = "bigcustomer/update"
            }
            bigcustomer_form.form('submit', {
                url: url,
                success: function (data) {
                    console.log(data);
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('提示', "操作成功", 'info', function () {
                            cmdObj.cancel();
                            cmdObj.reloadBigCustomer();
                        });
                    } else {
                        $.messager.alert('提示', data.msg, 'error')
                    }
                }
            });
        },
        cancel: function () {
            bigcustomer_dialog.dialog("close");
        }
    };
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
});
function formatDate(value,row,index){
  var unixTimestamp = new Date(value);
    return unixTimestamp.toLocaleString();
}
function formatTraceState(value,row,index){
    if(value==1){
        return "已经追踪"
    }else{
        return "未追踪"
    }
}
function formatState(value,row,index){
    if(value==1){
        return "<span style='color: #00ee00'>"+"已签约"+"</span>";
    }else if(value==0){
        return "<span style='color:red'>"+"未签约"+"</span>";
    }else{
        return "<span style='color:grey'>"+"失效"+"</span>";
    }}
function formatmarketer(value,row,index){
   return value.username;
    }
function formattracer(value,row,index){
    return value.username;
}
function formatimportance(value,row,index){
    return value.name;
}function formatintention(value,row,index){
    return value.name;
}
function formatintentionSubject(value,row,index){
    return value.name;
}
function formatcontact(value,row,index){
    return value.name;
}